using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

namespace Nirvana
{
    public static class BigWorldEditorUtility
    {
        private static Dictionary<string, int> _resourcePaths = new Dictionary<string, int>();

        public static bool IsMesh(Transform node, out int matPathId, out int meshPathId)
        {
            matPathId = -1;
            meshPathId = -1;
            MeshRenderer renderer = node.GetComponent<MeshRenderer>();
            if (renderer == null) return false;
            matPathId = GenerateMaterial(renderer.sharedMaterial, 0);
            if (matPathId < 0)
            {
                Debug.LogError("Material 丢失：" + node.name);
                return false;
            }

            MeshFilter meshFilter = node.GetComponent<MeshFilter>();
            if (meshFilter == null) return false;
            meshPathId = GenerateMesh(meshFilter.sharedMesh, 0);
            if (meshPathId < 0)
            {
                Debug.LogError("Mesh 丢失：" + node.name);
                return false;
            }

            return true;
        }

        public static int GenerateMaterial(Material mat, int level)
        {
            if (mat == null) return -1;
            string path = $"Materials/{mat.name}";
            return GetPathIndex(path);
        }

        public static int GenerateMesh(Mesh mesh, int level)
        {
            if (mesh == null) return -1;
            string path = $"Mesh/{mesh.name}";
            return GetPathIndex(path);
        }

        public static int GetPathIndex(string path)
        {
            int index = 0;
            if (!_resourcePaths.TryGetValue(path, out index))
            {
                index = _resourcePaths.Count;
                _resourcePaths.Add(path, index);
            }

            return index;
        }

        public static List<string> GetPaths()
        {
            return new List<string>(_resourcePaths.Keys);
        }
    }
}